(function (fruit, ui) {
    fruit.define('fruit.ui.FormView', {
        properties:{
            source:{
                value:null
            },
            containerTemplate:{
                value:{
                    tag:'ul'
                }
            },
            itemTemplate:{
                value:{
                    tag:'li',
                    content:[
                        {
                            name:'label',
                            tag:'span'

                        },
                        {
                            name:'editor',
                            tag:'span'
                        }
                    ]
                }
            }
        },
        methods:{
            init:function (source) {
                this.source(source);
                this._root = ui.viewEngine.resolve(this.containerTemplate()).getRoot();
            },
            renderTo:function (el) {
                var itemTemplate = this.itemTemplate();
                var source = this.source();
                var props = source.__properties__, name, prop;
                var item, editor;

                this._root.empty();

                for (var i = 0, length = props.length; i < length; i++) {
                    name = props[i];
                    prop = source[name];
                    editor = ui.viewEngine.resolve({
                        tag:'input',
                        attr:{
                            type:'text'
                        }
                    });

                    new fruit.Binding(editor.getRoot(), 'text', name, source);
                    item = ui.viewEngine.resolve(itemTemplate);
                    item.getElement('label').text(name);
                    item.getElement('editor').appendChild(editor.getRoot());
                    this._root.appendChild(item.getRoot());
                }

                this._root.appendTo(el);
            }
        }
    });
})(fruit, fruit.ui);
